package com.habdqn.dubbo.spring.boot.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.habdqn.dubbo.spring.boot.demo.dao.DetailDao;
import com.habdqn.dubbo.spring.boot.demo.entity.Detail;
import com.habdqn.dubbo.spring.boot.demo.service.DetailService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 帖子明细表(Detail)表服务实现
 *
 * @author xing.jiejian
 */
@Component
@Service(version = "1.0.0")
public class DetailServiceImpl implements DetailService {

    @Autowired
    private DetailDao detailDao;

    @Override
    public Detail queryById(Integer id) {
        return detailDao.selectById(id);
    }

    @Override
    public IPage<Detail> queryAllByPage(int pageNo, int pageSize) {
        return detailDao.selectPage(new Page<Detail>(pageNo,pageSize),null);
    }

    @Override
    public IPage<Detail> findPageBySortId(int pageNo,int pageSize,int sortId){
        IPage<Detail> page = new Page<>(pageNo,5);
        //如果分类ID为0，代表查询所有分类
        Wrapper<Detail> wrapper =null;
        if(sortId!=0) {
            wrapper = new QueryWrapper<Detail>().eq("sortId", sortId);
        }
        return detailDao.selectPage(page,wrapper);
    }

    @Override
    public Detail insert(Detail detail) {
        return detailDao.selectById(detailDao.insert(detail));
    }

    @Override
    public Detail update(Detail detail) {
        return detailDao.selectById(detailDao.updateById(detail));
    }

    @Override
    public boolean deleteById(Integer id) {
        return detailDao.deleteById(id)>0?true:false;
    }
}
